﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using zs.Notifier.PersistenceStore;
using Microsoft.SharePoint.Administration;

namespace zs.Notifier.ApplicationPages
{
    public partial class Settings : LayoutsPageBase
    {
        protected WebApplicationSelector uxWebApplicationSelector;
        protected InputFormTextBox uxNotification;
        protected InputFormCheckBox uxDisplay;
        protected InputFormCheckBox uxSticky;

        private SPWebApplication webApplication;
        private Notification notification;

        protected override void OnLoadComplete(EventArgs e)
        {
            Initialize();
            if (notification != null)
            {
                uxNotification.Text = notification.Message;
                uxSticky.Checked = notification.Sticky;
                uxDisplay.Checked = notification.Display;
            }            

            base.OnLoadComplete(e);
        }


        public void BtnSubmit_Click(object sender, EventArgs e)
        {
            Initialize();

            if (notification == null)
            {
                notification = new Notification("Notify", webApplication, Guid.NewGuid());
                notification.Update();
            }

            notification.Message = uxNotification.Text.ToString();
            notification.Sticky = uxSticky.Checked;
            notification.Display = uxDisplay.Checked;
            notification.Update();

            this.Page.Response.Redirect("/applications.aspx");

        }

        private void Initialize()
        {
            if (uxWebApplicationSelector != null)
            {
                webApplication = uxWebApplicationSelector.CurrentItem;
                if (webApplication != null)
                {
                    notification = webApplication.GetChild<Notification>("Notify");
                }
            }
        }
    }
}